<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userForm" label-width="220px" :rules="rules" :model="userInfo">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号" prop="workNumber">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              style="width: 300px"
              type="date"
              class="inputW"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门" prop="departmentName">
            <el-input v-model="userInfo.departmentName" class="inputW" />
          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式" prop="formOfEmployment">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option
                v-for="item in EmployeeEnum.hireType"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <upload-img ref="loadImg" :src="userInfo.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="updateInfo">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { saveUserInfoAPI, getUserDetailInfo } from '@/api/user'
import EmployeeEnum from '@/constant/employees'
export default {
  name: 'UserInfo',
  data() {
    return {
      userInfo: {}, // 用户信息数据
      EmployeeEnum,
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ]
      }
    }
  },
  async created() {
    // 获取用户信息API的调用
    const data = await getUserDetailInfo(this.$route.query.id)
    // 赋值给变量
    this.userInfo = data.data
    // 把数据中的图片地址,赋值给子组件中图片地址,实现刷新后,图片仍然存在
    this.$refs.loadImg.imageUrl = data.data.staffPhoto
  },
  methods: {
    // 点击更新按钮,触发事件
    async  updateInfo() {
      // 进行兜底验证
      this.$refs.userForm.validate(async bool => {
        if (!bool) return
        this.userInfo.staffPhoto = this.$refs.loadImg.imageUrl
        // 调用更新用户信息接口
        const res = await saveUserInfoAPI(this.userInfo.id, this.userInfo)
        console.log(res)
        // 如果编辑的是当前登录的账号,保存之后,保存到vuex中
        if (this.$store.getters.userId === this.userInfo.id) {
          console.log(123)
          // 保存到vuex中
          this.$store.commit('user/getUserInfo', res.data)
        }
        // console.log(res)
        // 信息提示
        this.$message.success(res.message)
        this.$router.back()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.user-info{
    padding-top:20px;
  }
</style>
